Linux Journal October 2017 by Linux Journal

Linux Journal October 2017 by Linux Journal

Author:Linux Journal
Language: eng
Format: mobi, epub
Tags: Web, Networking, BirdCam, Subutai, Travel, Apps, Research, Camera, headset, console, Command-Line, threading, Python, AV2000, drivers, ASCII art, Automation, Wireless, Applications, Ansible, Ubuntu, Data, Bash, website, Windows, Science, Linux Journal, Linux, Programming, Playbooks, laptops, conda, Wi-Fi, Scripting, SmartThings, DSLR, Lock Manager, CAPTCHA, TP-Link, Program, gphoto2, Anaconda, Powerline, PHP, Tools, Cloud
Publisher: Belltown Media
Published: 2017-09-27T07:00:00+00:00


- hosts: webservers

become: yes

tasks:

- name: install apache2

apt: name=apache2 state=latest update_cache=yes

when: ansible_os_family == "Debian"

- name: install httpd

yum: name=httpd state=latest

when: ansible_os_family == "RedHat"

- name: start apache2

service: name=apache2 state=started enable=yes

when: ansible_os_family == "Debian"

- name: start httpd

service: name=httpd state=started enable=yes

when: ansible_os_family == "RedHat

- name: install index

template:

src: index.html.j2

dest: /var/www/html/index.html

Here’s the template file, which must end in .j2 (it’s the file referenced in the last task above):

<html><center>

<h1>This computer is running {{ ansible_os_family }},

and its hostname is:</h1>

<h3>{{ ansible_hostname }}</h3>

{# this is a comment, which won't be copied to the index.html file #}

</center></html>

This also should be fairly easy to understand. The playbook takes a few different things it learned and installs Apache on the remote systems, regardless of whether they are Red Hat- or Debian-based. Then, it starts the web servers and makes sure the web server starts on system boot. Finally, the playbook takes the template file, index.html.j2, and substitutes the variables while copying the file to the remote system. Note the {# #} format for making comments. Those comments are completely erased on the remote system and are visible only in the .j2 file on the Ansible machine.

The Sky Is the Limit!

I’ll finish up this series in my next article, where I plan to cover how to build on your playbook knowledge to create entire roles and take advantage of the community contributions available. Ansible is a very powerful tool that is surprisingly simple to understand and use. If you’ve been experimenting with ad-hoc commands, I encourage you to create playbooks that will allow you to do multiple tasks on a multitude of computers with minimal effort. At the very least, play around with the “Facts” gathered by the ansible-playbook app, because those are things unavailable to the ad-hoc mode of Ansible. Until next time, learn, experiment, play and have fun!■

Shawn Powers is the Associate Editor for Linux Journal. He’s also the Gadget Guy for LinuxJournal.com, and he has an interesting collection of vintage Garfield coffee mugs. Don’t let his silly hairdo fool you, he’s a pretty ordinary guy and can be reached via e-mail at [email protected]. Or, swing by the #linuxjournal IRC channel on Freenode.net.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.